import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/models/base_advanced_filter_model.dart';

enum AdvancedFilterType { Radio, MultipleCheckbox, InputDateRange, InputRange }

class BaseFilterDataProvider {

  /// 数据源
  Map<String, dynamic> dataSource;

  /// 转化为model
  List<AdvancedFilterModel> _models;
  List<AdvancedFilterModel> get models => this._models.where((element) => element.tagAppShow == true).toList();

  /// 日期选项
  List<Map<String, dynamic>> dateRangeList;

  BaseFilterDataProvider({@required List<dynamic> filterForm, @required this.dataSource})
      : assert(filterForm != null),
        assert(dataSource != null) {
    this._models = filterForm.map((e) => AdvancedFilterModel.fromJson(e)).toList();
    _initDataSource();
    _generateDateRange();
  }

  BaseFilterDataProvider.create({@required List<AdvancedFilterModel> models, @required this.dataSource})
      : assert(models != null),
        assert(dataSource != null) {
    this._models = models;
    _initDataSource();
    _generateDateRange();
  }

  static Map<String, dynamic> dataEmptyFilter(Map<String, dynamic> data) {
    Map<String, dynamic> _newData = Map.from(data);
    _newData.removeWhere((key, value) {
      return value == null || value.toString() == '-2' || value.toString() == ['-2'].toString() || value == [] || value.toString().isEmpty;
    });
    return _newData;
  }

  void _initDataSource() {
    for (AdvancedFilterModel item in this.models) {
      item.tagIsExpanded = false;
      AdvancedFilterType type = BaseTransformUtil.enumFromString(AdvancedFilterType.values, item.tagType);
      switch (type) {
        case AdvancedFilterType.Radio:
          dataSource.addAll({item.tagId: '-2'});
          break;
        case AdvancedFilterType.MultipleCheckbox:
          dataSource.addAll({
            item.tagId: ['-2']
          });
          break;
        case AdvancedFilterType.InputDateRange:
          dataSource.addAll({item.tagId: null});
          break;
        case AdvancedFilterType.InputRange:
          dataSource.addAll({item.tagId: null});
          break;
      }
    }
  }

  void _generateDateRange() {
    DateFormat dateFormat = DateFormat('yyyy-MM-dd');
    DateTime now = DateTime.now();
    String toDay = dateFormat.format(now);
    String toMonth = dateFormat.format(DateTime(now.year, now.month));
    String within30Days = dateFormat.format(now.add(Duration(days: -30)));
    String within3Months = dateFormat.format(DateTime(now.year, now.month - 3, now.day));
    dateRangeList = [
      {'label': '不限', 'type': 'all', 'value': null},
      {
        'label': '本日',
        'type': 'today',
        'value': [toDay, toDay]
      },
      {
        'label': '本月',
        'type': 'toMonth',
        'value': [toMonth, toDay]
      },
      {
        'label': '最近30天',
        'type': 'within30Days',
        'value': [within30Days, toDay]
      },
      {
        'label': '最近3个月',
        'type': 'within3Months',
        'value': [within3Months, toDay]
      },
      {'label': '自定义', 'type': 'custom', 'value': ''},
    ];
  }
}
